/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.freemind;

import com.googlecode.mindlistening.gui.NewTagDialog;
import freemind.modes.HistoryInformation;
import freemind.modes.MindMapNode;
import freemind.modes.mindmapmode.MindMapController;
import freemind.modes.mindmapmode.hooks.MindMapHookAdapter;
import freemind.view.mindmapview.NodeView;
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.tree.TreeNode;

/**
 *
 * @author ryan
 */
public class TopicDialogHook  extends MindMapHookAdapter {
    @Override
    public void startupMapHook() {
        super.startupMapHook();
        final MindMapController controller = getMindMapController();
        ComboBoxModel tagdialog = new DefaultComboBoxModel();
        try {
            tagdialog = controller.getModel().getRegistry().getAttributes().getDefaultComboBoxModel("topic");
        } catch (Exception ex) {

             Logger.getLogger(MindListeningHook.class.getName()).log(Level.SEVERE, null, ex);
        }
        final NewTagDialog dialog = new com.googlecode.mindlistening.gui.NewTagDialog( controller.getFrame().getJFrame(), true, tagdialog);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        if (dialog.getTagAndDescription() != null) {

              final MindMapNode child = controller.addNew(controller.getSelected(), MindMapController.NEW_CHILD, null);
             //NodeView nodeView = controller.getNodeView(child);
             //controller.select(nodeView);
            HistoryInformation history = new freemind.modes.HistoryInformation(dialog.getTagAndDescription().getTagDate(), dialog.getTagAndDescription().getTagDate());
            child.setHistoryInformation(history);

            Timer t = new Timer();
            t.schedule(new TimerTask() {
                @Override
                public void run() {
                   
                     Color c = new Color(90,130,40);
                     child.setColor(c);
                     child.setText( dialog.getTagAndDescription().getTag());

                    controller.nodeStructureChanged(child.getParentNode());
                    controller.nodeChanged(child);
                    controller.nodeRefresh(child);
                    NodeView nodeView = controller.getNodeView(child);
                    controller.select(nodeView);
                }

            }, 200);

            try {
                // add to the registry
                controller.getModel().getRegistry().getAttributes().getElement("topic").addValue(dialog.getTagAndDescription().getTag());
            } catch (Exception ex) {
                 Logger.getLogger(MindListeningHook.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

/*
 * tags = c.model.registry.attributes.getDefaultComboBoxModel("topic");
dialog = new com.googlecode.mindlistening.gui.NewTagDialog( c.frame.getJFrame(), true, tags);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
if (dialog.tagAndDescription != null) {
    tagDetails = dialog.tagAndDescription;
    c.getController().obtainFocusForSelected();


     targetNode = c.getController().getView().getSelected().getModel();
     child = null;
     if (!targetNode.isRoot()) {
         parent = targetNode.getParentNode();
         int childPosition = parent.getChildPosition(targetNode);
         childPosition++;
         child = c.newChild.addNewNode(parent, childPosition, targetNode.isLeft());
         nodeView = c.getNodeView(child);
         c.select(nodeView);
     } else {
         parentFolded = targetNode.isFolded();
         if (parentFolded) {
            c.setFolded(targetNode,false);
         }
         position = c.getFrame().getProperty("placenewbranches").equals("last") ?
         targetNode.getChildCount() : 0;
         child = c.newChild.addNewNode(targetNode, position);
         nodeView = c.getNodeView(child);
         c.select(nodeView);
     }
    if (tagDetails.description != null && !"".equals(tagDetails.description)) {
        child.setText(tagDetails.description);
        child.createAttributeTableModel();
        if (child.getAttributes()==null || child.getAttributes().getRowCount()==0) {
            child.createAttributeTableModel();
        };
        child.getAttributes().insertRow(0 , 'tag', tagDetails.tag);

    } else {
        child.setText( tagDetails.tag);
    }
    history = new freemind.modes.HistoryInformation(tagDetails.tagDate, tagDetails.tagDate);
    child.setHistoryInformation(history)
    c.nodeStructureChanged(node);

}
 */

    }

}
